package taskflow.springframework.context.support;

import java.io.IOException;
import java.util.Properties;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertiesPropertySource;
import taskflow.constants.Parameter;

/**
 * 将Taskflow配置信息加入到Environment
 * 
 * @author bailey
 * @date 2022年2月14日
 */
public class PropsToEnvironment extends PropertySourcesPlaceholderConfigurer{
	private ConfigurableEnvironment environment;
	@Override
	public void setEnvironment(Environment environment) {
		this.environment = (ConfigurableEnvironment)environment;
		super.setEnvironment(environment);
	}

	@Override
	protected Properties mergeProperties() throws IOException {
		Properties props = super.mergeProperties();
		Properties taskFlowParameters = new Properties();
		props.forEach((k,v)->{
			if (k != null && Parameter.isLegal(k.toString())) {
				taskFlowParameters.put(k,v);
			}
		});
		environment.getPropertySources().addFirst(new PropertiesPropertySource("taskflowConfig",taskFlowParameters));
		return props;
	}
}
